package com.phamloc.ga01.objects;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.phamloc.ga01.manager.ResourcesManager;

public abstract class People extends AnimatedSprite{
	private Body body;
	private boolean canGo = false;
	public People(float pX, float pY, VertexBufferObjectManager vbo, Camera camera, PhysicsWorld physicsWorld)
    {
        super(pX, pY, ResourcesManager.getInstance().people_go_region, vbo);
        createPhysics(camera, physicsWorld);
    }
	public abstract void onStoped();
	private void createPhysics(Camera camera, PhysicsWorld physicsWorld) {
		body = PhysicsFactory.createBoxBody(physicsWorld, this, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(0, 0, 0));
		body.setUserData("people");
		body.setFixedRotation(true);
		camera.setChaseEntity(this);
		physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, false){
			@Override
	        public void onUpdate(float pSecondsElapsed)
	        {
	            super.onUpdate(pSecondsElapsed);
	            
	            if (getY() <= 0 || getY() >= 4000)
	            {                    
	            	onStoped();
	            }
	            
	            if (canGo)
	            {    
	                body.setLinearVelocity(new Vector2(5, body.getLinearVelocity().y)); 
	            }
	        }
		});
		setGo();
	}
	public void setGo()
	{
	    canGo = true;
	        
	    final long[] PLAYER_ANIMATE = new long[] { 150, 150, 150 , 150 };
	        
	    animate(PLAYER_ANIMATE, 0, 3, true);
	}
}
